package nix.mall.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import nix.mall.common.pojo.NixMallResult;
import nix.mall.pojo.User;
import nix.mall.sso.service.UserService;
import nix.mall.utils.CookieUtils;
import nix.mall.utils.JsonUtils;

/**
 * 用户处理Controller
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@Value("${TOKEN_KEY}")
	private String TOKEN_KEY;
	
	@RequestMapping("/user/check/{param}/{type}")
	@ResponseBody
	public NixMallResult checkData(@PathVariable String param, @PathVariable Integer type) {
		NixMallResult result = userService.checkData(param, type);
		return result;
	}
	
	@RequestMapping(value="/user/register", method=RequestMethod.POST)
	@ResponseBody
	public NixMallResult register(User user) {
		NixMallResult result = userService.register(user);
		return result;
	}
	
	@RequestMapping(value="/user/login", method=RequestMethod.POST)
	@ResponseBody
	public NixMallResult login(String username, String password,
			HttpServletRequest request, HttpServletResponse response) {
		NixMallResult result = userService.login(username, password);
		if (result.getStatus() == 200) {
			// 把token写入cookie
			CookieUtils.setCookie(request, response, TOKEN_KEY, result.getData().toString());
		}		
		return result;
	}
	
	@RequestMapping(value="/user/token/{token}", method=RequestMethod.GET,
			// 指定返回响应数据的Content-Type
			produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String getUserByToken(@PathVariable String token, String callback) {
		NixMallResult result = userService.getUserByToken(token);
		// 判断是否为Jsonp请求
		if (StringUtils.isNotBlank(callback)) {
			return callback + "("+ JsonUtils.objectToJson(result) +")";
		}
		return JsonUtils.objectToJson(result);
	}
	
	@RequestMapping(value="/user/token_v2/{token}", method=RequestMethod.GET)
	@ResponseBody
	// Jsonp处理第二种方式，要求Spring4.1以上
	public Object getUserByTokenV2(@PathVariable String token, String callback) {
		NixMallResult result = userService.getUserByToken(token);
		// 判断是否为Jsonp请求
		if (StringUtils.isNotBlank(callback)) {
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			// 设置回调方法
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		}
		return result;
	}
	
	@RequestMapping("/user/logout/{token}")
	@ResponseBody
	public NixMallResult logout(@PathVariable String token) {
		NixMallResult result = userService.logout(token);
		return result;
	}
}
